/*
######################################################################################################
#                                                                                                    # 
#	This Web Control implement for send mails                                                        # 
#	Author: Sharvan                                                                                  #     
#	Date: 16/03/06                                                                                   # 
#	                                                                                                 # 
######################################################################################################
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Net.Mail;

/// <summary>
/// This Control is used to send mail from one doctor to another doctor
/// </summary>
public partial class includes_DoctorEmailSend : System.Web.UI.UserControl
{
    bool _sendMailStatus = false;
    string _reciverEmail = "";    
    public string reciverEmail
    {
        set { _reciverEmail = value; }
        get { return _reciverEmail; }
    }   
    public bool sendMailStatus
    {
        set { _sendMailStatus = value; }
        get { return _sendMailStatus; }
    }   


    protected void Page_Load(object sender, EventArgs e)
    {
        localFunction lFunction=new localFunction();
        if (ViewState["pageFromOrAction"] == null)
            ViewState["pageFromOrAction"] = Session["pageFromOrAction"].ToString();
        else
            Session["pageFromOrAction"] = "";

        // check if doctor logged in
        if (!lFunction.checkDoctorLogin(false) || ViewState["pageFromOrAction"].ToString() != "connect")
        {
            this.Visible = false;
        }
        if (ViewState["_reciverEmail"] != null)
        {
            _reciverEmail = ViewState["_reciverEmail"].ToString();
        }
        else
        {
            ViewState["_reciverEmail"] = _reciverEmail;
        }
    }
    protected void lnkMail_Click(object sender, EventArgs e)
    {
        if(txtSubject.Text.Trim().Length > 0 && txtMessage.Text.Trim().Length > 0)
        {
            // creating an instance of manager class
            Manager objmanager = new Manager(true,"ConnectionString");
          // send mail
            //objmanager.sendJMail(txtMessage.Text, this.reciverEmail,txtSubject.Text, Session["UserEmail"].ToString(), Session["LoginName"].ToString(), "");
           // objmanager.sendMail(txtMessage.Text, this.reciverEmail, Session["LoginName"].ToString(), txtSubject.Text, Session["UserEmail"].ToString(), ""); //Commented By Wahaj dated Jan 12, 2012
            string strMessageStatus = SendEmailToDoctorDentist(txtMessage.Text, Session["DentistDoctorEmail"].ToString(), Session["LoginName"].ToString(), txtSubject.Text, Session["UserEmail"].ToString());//Added By Wahaj dated Jan 12, 2012
            if (strMessageStatus == "Send Successfully")
            {
                //Response.Write("Message Sent Successfully");
                lblMessage.Text = "<br>Mail has been succesfully sent to this Doctor.<br>";
                _sendMailStatus = true;
            }
            else
            {
                // Response.Write(strMessageStatus);
                lblMessage.Text = "<br>" + strMessageStatus + "<br>";
            }
            
           
        }
        else if(txtSubject.Text.Trim().Length <= 0)
        {
            lblMessage.Text = "<br>Please enter Subject<br>";
        }
        else if (txtMessage.Text.Trim().Length <= 0)
        {
            lblMessage.Text = "<br>Please enter Message<br>";
        }        
    }
    protected string SendEmailToDoctorDentist(string strBody, string strTo, string fromName, string strSubject, string strFrom)
    {
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {
            message.From = new MailAddress(strFrom);
            message.Subject = strSubject;
            message.Body = strBody;
            message.IsBodyHtml = true;
            // smtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtpClient.Host = "smtp.1and1.com"; //Or Your SMTP Server Address
            smtpClient.Credentials = new System.Net.NetworkCredential("docfriend@findadoc.com", "rateddoc");
            message.To.Add(strTo);
            smtpClient.EnableSsl = true;
            smtpClient.Send(message);
            message = null;
            smtpClient = null;
            return "Send Successfully";
            //            The Host address is
            //www.1and1.com

            //username and password for the Domain Web Mail is
            //docfriend@findadoc.com
            //rateddoc


        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }


}
